package com.woniuxy.yygh.hsop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.yygh.hsop.repository.DepartmentRepository;
import com.woniuxy.yygh.hsop.service.DepartmentService;
import com.woniuxy.yygh.model.hosp.Department;
import com.woniuxy.yygh.model.hosp.Hospital;
import com.woniuxy.yygh.vo.hosp.DepartmentQueryVo;
import com.woniuxy.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/3/1:20:23
 * <P> Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * TODO:  上传医院科室
     **/
    @Override
    public void save(Map<String, Object> parameterMap) {
        //map转为 Department 对象
        String mapString = JSONObject.toJSONString(parameterMap);
        Department department = JSONObject.parseObject(mapString, Department.class);

        //判断是否存在相同数据
        String hoscode = department.getHoscode();

        //查找获得部门
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(
                department.getHoscode(), department.getDepcode());


        //如果存在则更新
        if (departmentExist != null) {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);

        } else {
            //如果不存在 添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    /**
     * TODO:查询医院科室接口 分页
     **/
    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //创建 Pageable 分页对象
        Pageable pageable = PageRequest.of(page - 1, limit);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        //Example对象 条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * TODO: 根据科室编号 和 医院名称 删除科室
     **/
    @Override
    public void remove(String hoscode, String decode) {
        //医院编号 和科室编号 查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, decode);

        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }

    }

    /**
     * TODO:根据医院编号 查询所有的科室列表
     **/
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list 封装返回数据
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号 查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);

        Example<Department> example = Example.of(departmentQuery);
        //所有科室信息
        List<Department> departmentList = departmentRepository.findAll(example);

        //每个大科室的编号分组 bigcode 获取其下级子科室
        //大科室的编号 大科室对应的小科室信息
        Map<String, List<Department>> departmentMap =
                //分组 根据当前对象的编号
                departmentList
                        .stream()
                        //大科室 Bigcode
                        .collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {

            //大科室编号
            String bigcode = entry.getKey();

            //大科室下的小科室集合
            List<Department> department1List = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(department1List.get(0).getBigname());
            //封装小科室

            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : department1List) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());

                //封装到children
                children.add(departmentVo2);
            }
            //把小科室封装到大科室 children 里面  为了页面渲染
            departmentVo1.setChildren(children);

            //放到返回体里面
            result.add(departmentVo1);
        }

        return result;
    }


    /**
     * TODO:根据科室编号 医院编号 查询科室
     **/
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    /**
     * TODO:根据医院编号 医院编号 查询科室 返回对象
     **/
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
